package mk.edu.ii.navigatemacedonia;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.hardware.SensorManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

public class MapPreviewActivity extends MapActivity {
	
	private MapView mapView;
	List<Overlay> listOfOverlays ;
	SensorManager mSensorManager;
	private static Context context = null;
	
	/** Called when the activity is first created. 
	 * 
	 */
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.mappreviewactivity);
		context = getApplicationContext();
		
		mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
		
		mapView = (MapView)findViewById(R.id.map_view);
		
		listOfOverlays = mapView.getOverlays();
		mapView.setStreetView(false);
		mapView.setSatellite(true); 
		mapView.setTraffic(false);
		
		mapView.setBuiltInZoomControls(true);
		
        //setContentView(mView);
		goToMyLocation();       
        
	}
	
	@Override
	protected void onPause() {
		super.onPause();
	}
	/** Called when the Menu is created.
	 * 
	 */
	@Override
    public boolean onCreateOptionsMenu(Menu menu){
    	super.onCreateOptionsMenu(menu);
    	
    	SubMenu mapMode = menu.addSubMenu("MapMode");
    	
    	
    	mapMode.add(1, 1, Menu.NONE, "Street View");
    	mapMode.add(1, 2, Menu.NONE, "Satellite");
    	mapMode.add(1, 3, Menu.NONE, "Traffic");
    	mapMode.setGroupCheckable(1, true, true);
    	
    	mapMode.setGroupCheckable(2, true, true);
    	return true;
    } 
	
	
	public void goToMyLocation(){
		
		Intent intent = getIntent();
		
		Double longitude = intent.getDoubleExtra("longitude", 0d)*1E6;
		Double latitude = intent.getDoubleExtra("latitude", 0d)*1E6;
		
		GeoPoint point1= new GeoPoint(latitude.intValue(),longitude.intValue());
		
		Site site = (Site)intent.getSerializableExtra("site");
		GeoPoint point2 = new GeoPoint(((Double)(site.getSiteLatitude()*1E6)).intValue(),((Double)(site.getSiteLongitude()*1E6)).intValue());
		MapOverlayPoints overlay= new MapOverlayPoints(point1,point2,site.getSiteName());
		mapView.getController().animateTo(new GeoPoint((point1.getLatitudeE6()+point2.getLatitudeE6())/2,(point1.getLongitudeE6()+point2.getLongitudeE6())/2));
				
        mapView.getController().zoomToSpan(Math.abs(point1.getLatitudeE6()-point2.getLatitudeE6()), Math.abs(point1.getLongitudeE6()-point2.getLongitudeE6()));
        
        listOfOverlays.add(overlay);
       // listOfOverlays.add(new MapOverlayLeftCorner());
        mapView.postInvalidate();
	}
	/** Called when we select item from Menu.
	 * 
	 */
	public boolean onOptionsItemSelected(MenuItem item){
    	super.onOptionsItemSelected(item);
    	switch (item.getItemId()) {
       	case 1:{
       		mapView.setStreetView(true);
       		mapView.setSatellite(false);
       		mapView.setTraffic(false);
       		return true;
       	}
       	case 2:{

       		mapView.setStreetView(false);
       		mapView.setSatellite(true);
       		mapView.setTraffic(false);
       		return true;
       	}
       	case 3:{
       		mapView.setStreetView(false);
       		mapView.setSatellite(false);
       		mapView.setTraffic(true);
       		return true;
       	}
    	}
    	return true;
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// IMPORTANT: This method must return true if your Activity
		// is displaying driving directions. Otherwise return false.
		return false;
	}

	public MapPreviewActivity  pointer()
	{
		return this;
	}	
	
	public static void startNavigateActivity(double latitude, double longitude){
		Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=" + latitude/1E6 + "," + longitude/1E6));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}
}	

